package com.wwwarehouse.contract.contract_address;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.ClearEditText;
import com.wwwarehouse.common.custom_widget.address_dialog.DialogSelectAddressUpdate;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.bean.AddressListBean;
import com.wwwarehouse.contract.bean.UpdateAddressBean;
import com.wwwarehouse.contract.contract.TheParentFragment;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.event.AddAddressForConfirmEvent;
import com.wwwarehouse.contract.event.RefreshShoppCardEvent;

import java.util.HashMap;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * Created by feng.gao on 2017/10/19.
 * 新增地址
 */

public class UpdateAddressFragment extends TheParentFragment implements View.OnClickListener {
	private ClearEditText mConvEdt;
	private ClearEditText mPhoneEdt;
	private TextView mAddressTv;
	private Button mButton;
	private ClearEditText mAddressDetailEdt;
	private AddressListBean.ListBean addressBean;
	private DialogSelectAddressUpdate dialogSelectAddressUpdate;
	private String mProvince;
	private String mCity;
	private String mCounty;
	private String mDemanId;
	private String mAddressUkid;
	private String mType;
	private boolean isConv;
	private boolean isPhone;
	private boolean isDetail;
	private boolean isAddress;
	@Override
	protected int getContentId() {
		return R.layout.new_address_fragment;
	}

	@Override
	protected void init(View view) {
		mConvEdt = (ClearEditText) view.findViewById(R.id.conversion_edt);
		mPhoneEdt = (ClearEditText) view.findViewById(R.id.phone_edt);
		mAddressTv = (TextView) view.findViewById(R.id.address_tv);
		mAddressTv.setOnClickListener(this);
		mAddressDetailEdt = (ClearEditText) view.findViewById(R.id.address_detail_edt);
		mButton = (Button) view.findViewById(R.id.btn_confirm);
		mButton.setOnClickListener(this);
		mConvEdt.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				if (StringUtils.isNoneNullString(s.toString())) {
					isConv = true;
				} else {
					isConv = false;
				}
				checkCanClick();


			}
		});
		mPhoneEdt.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				if (StringUtils.isNoneNullString(s.toString())) {
					isPhone = true;
				} else {
					isPhone = false;
				}
				checkCanClick();

			}
		});
		mAddressDetailEdt.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				if (StringUtils.isNoneNullString(s.toString())) {
					isDetail = true;
				} else {
					isDetail = false;
				}
				checkCanClick();

			}
		});

	}

	@Override
	public void setTitle() {
	}

	@Override
	public void requestDatas() {
		if (getArguments() != null && getArguments().getSerializable(ContractConstant.KEY_EDIT_ADDRESS_LIST_DAO) != null) {
			addressBean = (AddressListBean.ListBean) getArguments().getSerializable(ContractConstant.KEY_EDIT_ADDRESS_LIST_DAO);
			if (addressBean != null) {
				mProvince = addressBean.getProvince();
				mCity = addressBean.getCity();
				mCounty = addressBean.getCounty();
				if (StringUtils.isNoneNullString(addressBean.getAddress())) {
					String address = addressBean.getAddress();
					String replaceAddress = addressBean.getProvince() + addressBean.getCity() + addressBean.getCounty();
					String result = address.replace(replaceAddress, "");
					mAddressTv.setText(replaceAddress);
					mAddressDetailEdt.setText(result);
				}
				if (StringUtils.isNoneNullString(addressBean.getMobilePhone())) {
					mPhoneEdt.setText(addressBean.getMobilePhone());
				}
				if (StringUtils.isNoneNullString(addressBean.getReceiveName())) {
					mConvEdt.setText(addressBean.getReceiveName());
				}
			}
		}
		if (getArguments() != null && getArguments().getString(ContractConstant.KEY_CONSTRACT_PLACE_ORDER_DEMANID) != null) {
			mDemanId = getArguments().getString(ContractConstant.KEY_CONSTRACT_PLACE_ORDER_DEMANID);
		}
		if (getArguments() != null && getArguments().getString(ContractConstant.KEY_ADDRESS_ADDRESSUKID) != null) {
			mAddressUkid = getArguments().getString(ContractConstant.KEY_ADDRESS_ADDRESSUKID);
		}
		if (getArguments() != null && getArguments().getString(ContractConstant.KEY_CONTRACT_ADD_ADDRESS) != null) {
			mType = getArguments().getString(ContractConstant.KEY_CONTRACT_ADD_ADDRESS);
			mActivity.setTitle("新增地址");
			mButton.setEnabled(false);
			mButton.setBackground(mActivity.getResources().getDrawable(R.drawable.common_btn1_noclick_shape));
		} else {
			mActivity.setTitle("编辑地址");
			mButton.setEnabled(true);
			mButton.setBackground(mActivity.getResources().getDrawable(R.drawable.common_btn1_normal_shape));
		}

	}

	@Override
	public void onSuccess(CommonClass commonClass, int what) {
		if (what == 1) {
			if (commonClass.getCode().equals("0")) {
				//刷新管理地址界面
				String addressUkid = null;
				if (commonClass.getData() == null) {
					return;
				}
				UpdateAddressBean bean = JSON.parseObject(commonClass.getData().toString(), UpdateAddressBean.class);
				if (bean != null) {

					try {
						addressUkid = String.valueOf(bean.getAddressCommonUkid());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				if (StringUtils.isNoneNullString(mType)) {
					//第一次点击新建进入新增地址页的时候记录，用于是否地址默认
					sp.putBooleanValue(ContractConstant.SP_FIRST_ADD_ADDRESS, true);
				}
				popFragment();
				//刷新确认订单页面
				String address = mProvince + mCity + mCounty + mAddressDetailEdt.getText().toString().trim();

				String phone = mPhoneEdt.getText().toString().trim();
				String name = mConvEdt.getText().toString().trim();
				EventBus.getDefault().post(new AddAddressForConfirmEvent(addressUkid , address , phone , name));
				//刷新vewpager
				EventBus.getDefault().post(new RefreshShoppCardEvent("refresh" , 0));
			} else {
				toast(commonClass.getMsg());
			}
		}

	}

	@Override
	public void onReLoad(int code) {

	}

	@Override
	public void onClick(View v) {
		if (v == mAddressTv) {
			dialogSelectAddressUpdate = new DialogSelectAddressUpdate(mActivity, true);
			dialogSelectAddressUpdate.show();
			dialogSelectAddressUpdate.setListener(new DialogSelectAddressUpdate.AddressListener() {
				@Override
				public void setAddress(String s, String s1, String s2, String s3) {
					mProvince = s;
					mCity = s1;
					mCounty = s2;
					mAddressTv.setText(s + s1 + s2);
					checkCanClick();

				}
			});
		} else if (v == mButton) {
			Map<String, Object> map = new HashMap<>();
			map.put("address", mProvince + mCity + mCounty + mAddressDetailEdt.getText().toString().trim());
			if (addressBean != null && StringUtils.isNoneNullString(addressBean.getAddressCommonUkid())) {
				map.put("addressCommonUkid", addressBean.getAddressCommonUkid());
			} else if (StringUtils.isNoneNullString(mAddressUkid)) {
				map.put("addressCommonUkid", mAddressUkid);
			}
			map.put("city", mCity);
			if (addressBean != null && StringUtils.isNoneNullString(addressBean.getCountry())) {
				map.put("country", addressBean.getCountry());
			} else {
				map.put("country", "中国");
			}
			map.put("county", mCounty);
			map.put("demandId", mDemanId);
			map.put("mobilePhone", mPhoneEdt.getText().toString().trim());
			map.put("province", mProvince);
			map.put("receiveName", mConvEdt.getText().toString().trim());
			if (StringUtils.isNoneNullString(mType)) {
				map.put("type", mType);
			} else {
				map.put("type", "update");
			}

			httpPost(ContractConstant.ADDRESS_EDIT_METHOD , map , 1 , true , "正在保存...");
		}
	}

	private void checkCanClick() {
		if (mAddressTv.getText().toString().equals("选择省市区")) {
			isAddress = false;
		} else {
			isAddress = true;
		}
		if (isConv && isPhone && isDetail && isAddress) {
			mButton.setEnabled(true);
			mButton.setBackground(mActivity.getResources().getDrawable(R.drawable.common_btn1_normal_shape));
		} else {
			mButton.setEnabled(false);
			mButton.setBackground(mActivity.getResources().getDrawable(R.drawable.common_btn1_noclick_shape));
		}
	}
}
